﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoiffeurProject.DBMS.Tables;
using CoiffeurProject.DBMS.Entities;
using CoiffeurProject.DBMS.Utils;
using System.IO;

namespace CoiffeurProject.DBMS
{
    public class Database
    {
        Customers customers;
        Employees employees;
        Notifications notifications;

        public Database()
        {
            FileAndDirectoryCheck();

            LoadTables();
        }

        private void FileAndDirectoryCheck()
        {
            if (!Directory.Exists(Paths.DBTablesDirectory)) Directory.CreateDirectory(Paths.DBTablesDirectory);

            for (int i = 0; i < Paths.AllFilePaths.Length; i++)
                if (!File.Exists(Paths.AllFilePaths[i])) File.Create(Paths.AllFilePaths[i]);
        }

        private void LoadTables()
        {
            customers = new Customers(FileManagment.Load(Paths.CustomersFilePath), this);
            employees = new Employees(FileManagment.Load(Paths.EmployeesFilePath), this);

            notifications = new Notifications(this);
        }

        internal void SaveTables()
        {
            FileManagment.Save(Paths.CustomersFilePath, customers.ToStrings());
            FileManagment.Save(Paths.EmployeesFilePath, employees.ToStrings());
        }

        #region Properties

        public Customers Customers { get { return customers; } }
        public Employees Employees { get { return employees; } }
        public Notifications Notifications { get { return notifications; } }

        #endregion
    }

    internal static class Paths
    {
        internal static String DBTablesDirectory = "Data";
        internal static String CustomersFilePath = DBTablesDirectory + "/Part1.dat";
        internal static String EmployeesFilePath = DBTablesDirectory + "/Part2.dat";

        internal static String[] AllFilePaths = new String[] {
            CustomersFilePath, EmployeesFilePath
        };
    }
}
